package com.hiyee.api.controller;

import com.hiyee.response.ResponseJsonResult;
import com.hiyee.response.ResponseStatusEnum;
import com.hiyee.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;

/**
 * 公共控制器接口方法
 *
 * @author WangBin
 */
public class BaseController {

    @Autowired
    protected RedisOperator redisOperator;

    /**
     * 获取第一个参数验证错误
     *
     * @param BindingResult bindingResult
     * @return String
     */
    protected String getError(BindingResult bindingResult) {
        return bindingResult.getFieldErrors().get(0).getDefaultMessage();
    }

    /**
     * 返回成功
     *
     * @param Object data
     * @return ResponseJsonResult
     */
    protected ResponseJsonResult success(Object data) {
        return ResponseJsonResult.success(data);
    }

    /**
     * 返回失败
     *
     * @param ResponseStatusEnum responseStatusEnum
     * @return ResponseJsonResult
     */
    protected ResponseJsonResult error(ResponseStatusEnum responseStatusEnum) {
        return ResponseJsonResult.error(responseStatusEnum);
    }
}
